// General-purpose wrapper code that can be used as the base of a tui-lib
// program. Contained to reduce boilerplate and improve consistency between
// programs.

import {Root} from 'tui-lib/ui/primitives'

import {CommandLineInterface, Flushable} from './interfaces/index.js'
import * as ansi from './ansi.js'

export default async function tuiApp(callback) {
    // TODO: Support other screen interfaces.
    const screenInterface = new CommandLineInterface();

    const flushable = new Flushable(process.stdout, true);

    const root = new Root(screenInterface, flushable);

    const size = await screenInterface.getScreenSize();
    root.w = size.width;
    root.h = size.height;
    flushable.resizeScreen(size);
    root.on('rendered', () => flushable.flush());

    screenInterface.on('resize', newSize => {
        root.w = newSize.width;
        root.h = newSize.height;
        flushable.resizeScreen(newSize);
        root.fixAllLayout();
    });

    const cleanTerminal = function () {
        process.stdout.write(ansi.cleanCursor());
        process.stdout.write(ansi.disableAlternateScreen());
    };

    const dirtyTerminal = function () {
        process.stdout.write(ansi.enableAlternateScreen());
        process.stdout.write(ansi.startTrackingMouse());
    };

    const quitProgram = function (status = 0) {
        cleanTerminal();
        process.exit(status);
    };

    const suspendProgram = function () {
        cleanTerminal();
        process.kill(process.pid, 'SIGTSTP');
    };

    process.on('SIGCONT', () => {
        flushable.clearLastFrame();
        process.stdin.setRawMode(false);
        process.stdin.setRawMode(true);
        dirtyTerminal();
    });

    dirtyTerminal();

    try {
        return await callback({
            root,
            suspendProgram,
            quitProgram
        });
    } catch (error) {
        cleanTerminal();
        console.error(error);
        process.exit(1);
    };
};
